namespace JQuery.WebKit
{
    using System;
    using System.Web.Script.Serialization;

    internal class MultipleSearching : BaseSearching
    {
        private WebGrid _grid;
        private string _searchFilters;

        public MultipleSearching(WebGrid grid, string searchFilters)
            : base(grid)
        {
            this._grid = grid;
            this._searchFilters = searchFilters;
        }

        public SearchCriteriaCollection PerformSearch()
        {
            JsonMultipleSearch search = new JavaScriptSerializer().Deserialize<JsonMultipleSearch>(this._searchFilters);
            WebGridSearchEventArgs e = new WebGridSearchEventArgs(search.groupOp);
            foreach (MultipleSearchRule rule in search.rules)
            {
                e.CriteriaList.Add(new SearchCriteria(rule.field, rule.data, base.GetSearchOperationFromString(rule.op)));
            }
            this._grid.OnSearching(e);
            if (e.Cancel)
            {
                return null;
            }
            this._grid.OnSearched(new EventArgs());
            return e.CriteriaList;
        }
    }
}

